<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','image','remark','pattern','price','work_time','serve_time','times','cancel_time','ishot','paixu','createtime');
	$list=pdo_getslice('jy_qiuyuyue_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_qiuyuyue/sever//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
			array('op'=>'edit','title'=>'添加项目'),
		),
		'list'=>array('data'=>'list','width'=>'15','tip'=>'【温馨提示】：</br>1、这里设置场馆项目，比如：七人制足球，五人制足球!</br>2、添加场馆项目，点击《项目场地》，用于管理添加场地！','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','action'=>'/store.serve_plate/','title'=>'项目场地'),
			array('type'=>'href','action'=>'/store.time_pai/','title'=>'单独设置时间'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_qiuyuyue_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('open_total'=>0);
	}
	$item['hdps']=$item['hdps']?json_decode($item['hdps'],true):array();
	$item['fileds']=$item['fileds']?json_decode($item['fileds'],true):array();
	$item['fuwu']=$item['fuwu']?json_decode($item['fuwu'],true):array();
	$return['item']=$item;
	$return['item']['serve_time']=$return['item']['serve_time'] ? json_decode($return['item']['serve_time'],true):array();
	$return['item']['package']=json_decode($return['item']['package']);
	$return['item']['time_box']=$return['item']['time_box']?json_decode($return['item']['time_box'],true):array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'项目名称','name'=>'title','help'=>'','ismust'=>true),
								array('type'=>'image','title'=>'项目图片','name'=>'image','help'=>'','ismust'=>true),
								//array('type'=>'select','name'=>'categoryid','title'=>'项目类型','options'=>'category','option_title'=>'title'),
								//array('type'=>'checkbox','title'=>'服务方式','name'=>'serve_types','help'=>'','options'=>'serve_types'),
								array('type'=>'unitstring','unit'=>'元/场','title'=>'默认项目价格','name'=>'price','help'=>'','ismust'=>true),
								array('type'=>'string','title'=>'每场时长','name'=>'times','help'=>'仅填数字，单位:分钟','ismust'=>true),
								//array('type'=>'checkbox','title'=>'上班时间','name'=>'work_time','help'=>'','options'=>'work_time'),
								//array('type'=>'string','title'=>'可取消时间范围','name'=>'cancel_time','help'=>'仅填数字，距离服务时间在该小时之前可选择取消，单位:小时'),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>'设置热门'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'text','title'=>'项目说明','name'=>'remark','help'=>''),
								array('type'=>'checkbox','title'=>'场次时间','name'=>'work_time','help'=>'','options'=>'work_time'),
								array('type'=>'unitstring','unit'=>'元','title'=>'周六加价','name'=>'week6_addfee'),
								array('type'=>'unitstring','unit'=>'元','title'=>'周日加价','name'=>'week7_addfee'),
								array('type'=>'radio','title'=>'周六加价场次','name'=>'week6_addtime','help'=>'','options'=>'addtime'),
								array('type'=>'radio','title'=>'周日加价场次','name'=>'week7_addtime','help'=>'','options'=>'addtime'),
								array('type'=>'switch','title'=>'启用库存模式？','name'=>'open_total','help'=>'启用后，每个时间段每个场次，可以预约多人！'),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'场次时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',			
									'field1'=>'times',
									'title2'=>'价格',
									'field2'=>'fee',
									'field2unit'=>'元',
									'title3'=>'库存',
									'field3'=>'total',
									'field3unit'=>'人',
									'show'=>array('open_total'=>1)
								),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'场次时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',			
									'field1'=>'times',
									'title2'=>'价格',
									'field2'=>'fee',
									'field2unit'=>'元',
									'show'=>array('open_total'=>'0')
								),
							)
						),
						/*array(
							'title'=>'场地设置',
							'list'=>array(
								array('type'=>'array','name'=>'package','add_tip'=>'添加一个场地',
								'list'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'场地名称'),	
									array('type'=>'string','name'=>'package_price','title'=>'场地价格'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'场地名称','ismust'=>true),	
									array('type'=>'string','name'=>'package_price','title'=>'场地价格','ismust'=>true),
								)
								),
							)
						),*/
						array(
							'title'=>'自定义字段',
							'list'=>array(
								array('type'=>'array','name'=>'fileds','add_tip'=>'添加一个字段（温馨提示：已经有用户提交过数据的表单，不允许再编辑和添加新字段！）',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'字段名称'),	
										array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),
										array('type'=>'radio','name'=>'ismust','title'=>'必填','options'=>'ismust'),									
										array('type'=>'string','name'=>'paixu','title'=>'排序'),	
										array('type'=>'string','name'=>'value','title'=>'选项信息'),	
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'字段名称','ismust'=>true),	
										array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'type','option_title'=>'type','ismust'=>true),
										array('type'=>'switch','name'=>'ismust','title'=>'必填','help'=>''),
										array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
										array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
									)
								),
							)
						),
						array(
							'title'=>'项目幻灯片',
							'list'=>array(
								array(
									'type'=>'array',
									'name'=>'hdps',
									'add_tip'=>'添加一个幻灯片',
										'list'=>array(
											array('type'=>'image','name'=>'picurl','title'=>'幻灯片图片'),
											array('type'=>'string','name'=>'url','title'=>'幻灯片链接'),
											
										),
										'details'=>array(
											array('type'=>'image','name'=>'picurl','title'=>'幻灯片图片','help'=>''),
											array('type'=>'string','name'=>'url','title'=>'幻灯片链接'),
											
										)
							),
							)
						),
			
			)
	);
	$return['serve_types']=$this->serve_types;
	$return['addtime']=$this->addtime;
	$return['ismust']=array('0'=>'否','1'=>'是');
	//$return['category']=pdo_getall('jy_gohome_serve_category',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'));
	$return['work_time']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'serve_types'=>trim($postdata['serve_types']),
		'image'=>trim($postdata['image']),
		'price'=>intval($postdata['price']),
		'work_time'=>trim($postdata['work_time']),
		'serve_time'=>ijson_encode($postdata['serve_time']),
		'package'=>ijson_encode($postdata['package']),
		'cancel_time'=>intval($postdata['cancel_time']),
		'remark'=>trim($postdata['remark']),
		'times'=>intval($postdata['times']),
		'status'=>intval($postdata['status']),
		'ishot'=>intval($postdata['ishot']),
		'paixu'=>intval($postdata['paixu']),
		'hdps'=>ijson_encode($postdata['hdps']),
		'fileds'=>ijson_encode($postdata['fileds']),
		'store_id'=>$store_id,
		'week6_addfee'=>trim($postdata['week6_addfee']),
		'week6_addtime'=>intval($postdata['week6_addtime']),
		'week7_addfee'=>trim($postdata['week7_addfee']),
		'week7_addtime'=>intval($postdata['week7_addtime']),
		'fuwu'=>ijson_encode($postdata['fuwu']),
		'open_total'=>intval($postdata['open_total']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_qiuyuyue_serve',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_qiuyuyue_serve',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$is=pdo_getcolumn('jy_qiuyuyue_store_serve',array('serve_id'=>$id,'uniacid'=>$_SESSION['uniacid']),'id');
			if($is){
				exi('该项目已经被门店引用，不能删除！','error');
			}
			pdo_delete('jy_qiuyuyue_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_qiuyuyue_serve',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_qiuyuyue_serve',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='choose'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_qiuyuyue_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_qiuyuyue/sever//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}